package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.zycommunity.entity.*;
import com.blb.zycommunity.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import static net.sf.jsqlparser.parser.feature.Feature.update;

/**
 * <p>
 * 业主  前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
public class ZyOwnerController {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private IZyOwnerService ownerService;

    @Autowired
    private IZyCommunityService communityService;

    @Autowired
    private IZyBuildingService buildingService;

    @Autowired
    private IZyUnitService unitService;

    @Autowired
    private IZyRoomService roomService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private IZyOwnerRoomService ownerRoomService;

    @GetMapping("/owner-pages")
    public ResponseResult<IPage<ZyOwner>> findOwnerPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                       @RequestParam(required = false,defaultValue = "5") Long size,
                                                       @RequestParam(required = false) String communityId,
                                                       @RequestParam(required = false) String ownerNickname,
                                                       @RequestParam(required = false) String ownerRealName,
                                                       @RequestParam(required = false) String ownerIdCard,
                                                       @RequestParam(required = false) String ownerPhoneNumber){
        IPage<ZyOwner> ownerPage = ownerService.findOwnerPage(current, size,communityId, ownerNickname, ownerRealName, ownerIdCard, ownerPhoneNumber);
        return ResponseResult.ok(ownerPage);
    }



    @GetMapping("/audit-records")
    public ResponseResult<List<ZyOwner>> findAuditRecords(@RequestParam(required = false) String ownerRealName){
        List<ZyOwner> auditRecords = ownerService.findAuditRecords(ownerRealName);
        return ResponseResult.ok(auditRecords);
    }

    @PostMapping("owner-add")
    public ResponseResult addOwner(@RequestBody ZyOwner zyOwner){
        try {
            ownerService.register(zyOwner);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            return ResponseResult.error(401L,"用户名已存在");
        }
    }

    @GetMapping("/uni-app-owner-getRoom/{ownerNickname}")
    public ResponseResult<List<ZyOwner>> findRoom(@PathVariable String ownerNickname){
        List<ZyOwner> room = ownerService.findRoomByNickname(ownerNickname);
        return ResponseResult.ok(room);
    }

    @GetMapping("/uni-app-owner-getCommunity/{ownerNickname}")
    public ResponseResult<List<ZyOwner>> findCommunityId(@PathVariable String ownerNickname){
        List<ZyOwner> communityId = ownerService.findCommunityIdByNickname(ownerNickname);
        return ResponseResult.ok(communityId);
    }

    @GetMapping("/uni-app-getCommunity")
    public ResponseResult<List<ZyCommunity>> findAll(){
        List<ZyCommunity> community = communityService.findAllCommunity();
        return ResponseResult.ok(community);
    }

    @GetMapping("/uni-app-getUnit")
    public ResponseResult<List<ZyUnit>> findUnitByCommunityId(@RequestParam String buildingId){
        List<ZyUnit> unit = unitService.findUnitByBuildingId(buildingId);
        return ResponseResult.ok(unit);
    }

    @GetMapping("/uni-app-getBuilding")
    public ResponseResult<List<ZyBuilding>> findBuildingByUnitId(@RequestParam String communityId){
        List<ZyBuilding> buildingByCommunityId = buildingService.findBuildingByCommunityId(communityId);
        return ResponseResult.ok(buildingByCommunityId);
    }

    @GetMapping("/uni-app-getRoom")
    public ResponseResult<List<ZyRoom>> findRoomByBuilding(@RequestParam String unitId){
        List<ZyRoom> room = roomService.findRoomByBuildingId(unitId);
        return ResponseResult.ok(room);
    }

    @GetMapping("/uni-app-getType")
    public ResponseResult findOwnerType(String dataType){
        List<SysDictData> dictData = sysDictDataService.findDictData(dataType);
        return ResponseResult.ok(dictData);
    }

    @GetMapping("/uni-app-getOwnerId")
    public ResponseResult findOwnerId(@RequestParam String ownerNickname){
        ZyOwner zyOwner = ownerService.findByNickname(ownerNickname);
        return ResponseResult.ok(zyOwner);
    }

    @PutMapping("/owner-portrait")
    public ResponseResult updateOwnerPortrait(@RequestParam String ownerPortrait,
                                              @RequestParam String ownerId){
        ownerService.updateOwnerPortrait(ownerPortrait,ownerId);
        return ResponseResult.ok("ok");
    }


    @PutMapping("/uni-app-editOwner")
    public ResponseResult editOwner(@RequestBody ZyOwner owner){
        boolean update = ownerService.updateById(owner);
        return ResponseResult.ok(update);
    }

    @PutMapping("/uni-app-editPassword")
    public ResponseResult editPassword(@RequestParam String ownerNickname,
                                       @RequestParam String ownerPassword,
                                       @RequestParam String newPassword){
        ZyOwner zyOwner = ownerService.findByNickname(ownerNickname);
        String password = zyOwner.getOwnerPassword();
        //判断输入的原密码和加密后的密码是否一致
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        boolean matches = bc.matches(ownerPassword,password);
        if(matches){
            zyOwner.setOwnerPassword(bCryptPasswordEncoder.encode(newPassword));
            boolean update = ownerService.updateById(zyOwner);
            return ResponseResult.ok(update);
        }else {
            //提示密码修改失败
            return  ResponseResult.error(403L,"旧密码错误");
        }
    }

    @GetMapping("/uni-app-getRoomByOwnerId")
    public ResponseResult<List<ZyOwnerRoom>> findByOwnerId(@RequestParam String ownerId){
        List<ZyOwnerRoom> byOwnerId = ownerRoomService.findByOwnerId(ownerId);
        return ResponseResult.ok(byOwnerId);
    }
}
